package com.xtaller.test.api.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xtaller.test.R;
import com.xtaller.test.common.util.IdHelper;
import com.xtaller.test.common.util.JsonHelper;
import com.xtaller.test.common.util.TimeHelper;
import com.xtaller.test.model.BaseUser;
import com.xtaller.test.service.BaseUserService;
import com.xtaller.test.service.UserService;
import com.xtaller.test.vo.UserVo;
import io.swagger.annotations.Api;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-09-04
 * @Description:
 */
@Api(tags = "测试用户api")
@RestController
@RequestMapping("/v1/test")
public class UserApi {
    @Autowired private BaseUserService userService;
    @Autowired private UserService usersService;

    @GetMapping("/user")
    public Object get(@RequestParam(name = "key", defaultValue = "") String key) {

        if(key.trim().length() == 0){
            var list = new ArrayList<BaseUser>();
            for(int i=0;i<5;i++){
                var tem = new BaseUser();
                // var name = i * 10;
                tem.setName("A" + i);

                tem.setAge(i);
                list.add(tem);
            }
            try{
                usersService.batch(list);

               // userService.batch(list);
            }catch (Exception x){
                System.err.println(x.getMessage());
            }


            return userService.list();
        }else{
            var qw = new QueryWrapper<BaseUser>();
            qw.lambda()
                    .select(BaseUser::getId, BaseUser::getName, BaseUser::getAge)
                    .like(BaseUser::getName, key)
                    .eq(BaseUser::getIsDelete, 0)
                    //.eq(BaseUser::getAge, 10)
                    .orderByDesc(BaseUser::getAge);
            var list = userService.list(qw);
            list = list.stream().filter(x -> x.getAge() == 10).collect(Collectors.toList());
            return list;
        }

    }
    @GetMapping("/page/user")
    public Object page(@RequestParam(name = "index", defaultValue = "1") int index,
                       @RequestParam(name = "size", defaultValue = "20") int size,
                       @RequestParam(name = "key", defaultValue = "") String key) {
        // 分页攻击处理
        if(index < 0){
            index = 1;
        }
        var p = new Page<BaseUser>(index, size);
        var qw = new QueryWrapper<BaseUser>();
        if(key.trim().length() > 0){
            // BaseUser::getName ==> new BaseUser().getName()
            qw.lambda().like(BaseUser::getName, key);
        }
        return userService.page(p, qw);

    }

    @GetMapping("/user/{id}")
    public Object getById(@PathVariable Long id) {
        return userService.getById(id);
    }


    @PostMapping("/user")
    public Object create(@RequestBody @Valid UserVo object) {
        var user = JsonHelper.o2m(object, BaseUser.class);
        var qw = new QueryWrapper<BaseUser>();
        qw.lambda().eq(BaseUser::getName, object.getName());
        var list = userService.list(qw);
        if(list.size() > 0){
            return "用户名存在";
        }
        user = userService.model(user);
        if(user != null){
            return user;
        }
        return "error";
    }
    @PutMapping("/user/{id}")
    public Object update(@RequestBody @Valid UserVo object, @PathVariable Long id) {
        // 必须先查询在修改
        var model = userService.getById(id);
        if(model == null){
            return "数据id异常";
        }

        var user = JsonHelper.o2m(object, BaseUser.class);
        user.setId(id);
        user = userService.model(user);

        if(user != null){
            return user;
        }

        return "error";
    }

}
